/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.guit.demo.client.microevents;

import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasText;
import com.google.inject.Inject;

import com.guit.client.GuitPresenter;
import com.guit.client.apt.GwtPresenter;
import com.guit.client.binder.Attribute;
import com.guit.client.binder.GuitBinder;
import com.guit.client.binder.ViewField;
import com.guit.client.binder.ViewHandler;
import com.guit.client.binder.contributor.HasAttribute;
import com.guit.client.binder.contributor.RunAsync;
import com.guit.client.place.Place;
import com.guit.demo.client.alerter.Alerter;
import com.guit.demo.client.common.HasInnerText;
import com.guit.demo.client.main.MainPresenter;
import com.guit.demo.client.microevents.MicroeventsPresenter.MicroeventsBinder;

@RunAsync
@GwtPresenter(placeName = "micro")
public class MicroeventsPresenter extends GuitPresenter<MicroeventsBinder> implements Place<Void> {

  public interface MicroeventsBinder extends GuitBinder<MicroeventsPresenter> {
  }

  private final MainPresenter hasContent;
  private final Alerter alerter;

  @ViewField
  HasHTML panel;

  @ViewField
  HasInnerText time;

  @ViewField
  HasText render;

  @Inject
  public MicroeventsPresenter(MainPresenter hasContent, Alerter alerter) {
    this.hasContent = hasContent;
    this.alerter = alerter;
  }

  @Override
  public void go(Void data) {
    hasContent.setContent(getView(), 4);
  }

  @ViewHandler
  @HasAttribute("info")
  void panel$click(@Attribute String info) {
    alerter.alert(info);
  }

  @ViewHandler
  void render$click() {
    long t = System.currentTimeMillis();
    StringBuilder sb = new StringBuilder();
    boolean clickeable = true;
    for (int n = 0; n < 10000; n++) {
      if (clickeable) {
        sb.append("<div info='");
        sb.append(n);
        sb.append("'> DIV ");
      } else {
        sb.append("<div> NOT CLICKEABLE ");
      }
      sb.append(n);
      sb.append("</div>");
      clickeable = !clickeable;
    }
    panel.setHTML(sb.toString());
    time.setInnerText((System.currentTimeMillis() - t) + "ms");
  }
}
